package hu.veilingsite.gui;

import java.sql.Date;
import java.util.ArrayList;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.shared.ClientUser;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ProfileAdminUsers {
	private ArrayList<ClientUser> users;
	private Label lblName,lblAddress,lblResidence,lblBirthDate,lblEmail,lbrEmail,lblCredits,lblMayOffer,lblMayBid,lbrCredits,lblPassword1,lblPassword2;
	private TextBox tbrName,tbrAddress,tbrResidence,tbrPassword1,tbrPassword2,boxbirthdateday,boxbirthdatemonth,boxbirthdateyear,boxCredits;
	private Button save;
	private CheckBox bid, offer;
	final ServiceAsync myService = (ServiceAsync)
	GWT.create(Service.class);
	final AsyncCallback<ArrayList<ClientUser>> callback = new AsyncCallback<ArrayList<ClientUser>>(){

		@Override
		public void onFailure(Throwable caught) {
			System.out.println("we made a mistake, we're sorry!");
			//moet nog een foutmelding komen
		}

		@Override
		public void onSuccess(ArrayList<ClientUser> result) {
			users = result;
			FlexTable table = new FlexTable();
			int i = 0;
			for(ClientUser cu : users){
				Label email = new Label();
				Button edit = new Button("Bewerk");
				email.setText(cu.getEmail());
				final ClientUser c = cu;
				edit.addClickHandler(new ClickHandler(){
					public void onClick(ClickEvent e){
						RootPanel main = RootPanel.get("main");
						main.clear();
						main.add(createPage2(c));
					}
				});
				table.setWidget(i, 0, email);
				table.setWidget(i, 1, edit);
				i += 1;
			}
			VerticalPanel p = new VerticalPanel();
			p.add(table);
			RootPanel main = RootPanel.get("main");
			main.clear();
			main.add(p);
		}
	};
	
	final AsyncCallback<Boolean> saveData = new AsyncCallback<Boolean>() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("er is iets mis gegaan bij het maken met een verbinding naar de server, probeer het later opnieuw");
		}
		@Override
		public void onSuccess(Boolean result) {
			if (result) {
				//Window.alert("User updated.");
			} else {
				//Window.alert("User not updated.");
			}
		}
	};

	public ProfileAdminUsers() {
	}

	public void createPage() {
		myService.getUsers(callback);
	}
	
	public Panel createPage2(final ClientUser u){
		//Panels
		HorizontalPanel p = new HorizontalPanel();
		HorizontalPanel p1 = new HorizontalPanel();
		HorizontalPanel p2 = new HorizontalPanel();
		//Labels
		lblName = new Label("Naam");
		tbrName = new TextBox();
		lblAddress = new Label("Adres");
		tbrAddress = new TextBox();
		lblResidence = new Label("Woonplaats");
		tbrResidence = new TextBox();
		lblBirthDate = new Label("Geboorte Datum");
		boxbirthdateday = new TextBox();
		boxbirthdateday.setVisibleLength(2);
		boxbirthdateday.setMaxLength(2);
		boxbirthdateday.addStyleName("sadbox");
		boxbirthdatemonth = new TextBox();
		boxbirthdatemonth.setVisibleLength(2);
		boxbirthdatemonth.setMaxLength(2);
		boxbirthdatemonth.addStyleName("sadbox");
		boxbirthdateyear = new TextBox();
		boxbirthdateyear.setVisibleLength(2);
		boxbirthdateyear.setMaxLength(2);
		boxbirthdateyear.addStyleName("sadbox");
		lblEmail = new Label("Email");
		lbrEmail = new Label();
		lblCredits = new Label("Credits");
		boxCredits = new TextBox();
		lbrCredits = new Label();
		lblMayOffer = new Label("Mag veilen");
		offer = new CheckBox();
		lblMayBid = new Label("Mag bieden");
		bid = new CheckBox();
		lblPassword1 = new Label("Nieuw Wachtwoord");
		tbrPassword1 = new TextBox();
		lblPassword2 = new Label("Herhaal Wachtwoord");
		tbrPassword2 = new TextBox();
		save = new Button("Opslaan");
		
		FlexTable tableDOB = new FlexTable();
		tableDOB.addStyleName("nicetable");
		
		tableDOB.setWidget(0,0,boxbirthdateday);
		tableDOB.setWidget(0,1,boxbirthdatemonth);
		tableDOB.setWidget(0,2,boxbirthdateyear);
		
		//save User
		save.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e){
				if (!tbrPassword1.getText().equals(tbrPassword2.getText())) {
					System.out.println("wachtwoorden zijn niet gelijk");
				} else {
					u.setAddress(tbrAddress.getText());
					u.setName(tbrName.getText());
					u.setResidence(tbrResidence.getText());
					//u.setBirthdate(new Date((Integer.parseInt(boxbirthdateyear.getText())-1900),(Integer.parseInt(boxbirthdatemonth.getText())-1),Integer.parseInt(boxbirthdateday.getText())));
					if (!tbrPassword1.getText().equals("")) {
						myService.updatePassword(tbrPassword1.getText(),u.getEmail(),saveData);
					}
					u.setCredits(Integer.parseInt(boxCredits.getText()));
					u.setMaybid(bid.getValue());
					u.setMayoffer(offer.getValue());
					myService.updateUser(u,saveData);
					createPage();
				}
			}
		});
		//adding to panels
		p1.add(boxbirthdateday);
		p1.add(boxbirthdatemonth);
		p1.add(boxbirthdateyear);
		
		p2.add(boxCredits);
		p2.add(lbrCredits);
		
		//adding to table
		FlexTable table = new FlexTable();
		table.addStyleName("nicetable");
		
		table.setWidget(0,0,lblName);
		table.setWidget(0,1,tbrName);
		table.setWidget(1,0,lblAddress);
		table.setWidget(1,1,tbrAddress);
		table.setWidget(2,0,lblResidence);
		table.setWidget(2,1,tbrResidence);
		//table.setWidget(3,0,lblBirthDate);
		//table.setWidget(3,1,tableDOB);
		table.setWidget(4,0,lblEmail);
		table.setWidget(4,1,lbrEmail);
		table.setWidget(5,0,lblCredits);
		table.setWidget(5,1,p2);
		table.setWidget(6,0,lblMayOffer);
		table.setWidget(6,1,offer);
		table.setWidget(7,0,lblMayBid);
		table.setWidget(7,1,bid);
		table.setWidget(8,0,lblPassword1);
		table.setWidget(8,1,tbrPassword1);
		table.setWidget(9,0,lblPassword2);
		table.setWidget(9,1,tbrPassword2);
		table.setWidget(10,0,save);
		
		//adding data
		tbrName.setText(u.getName());
		tbrAddress.setText(u.getAddress());
		tbrResidence.setText(u.getResidence());
		boxbirthdateday.setText(""+u.getBirthdate().getDate());
		boxbirthdatemonth.setText(""+(u.getBirthdate().getMonth()+1));
		boxbirthdateyear.setText(""+(u.getBirthdate().getYear()+1900));
		lbrEmail.setText(u.getEmail());
		boxCredits.setText("" + u.getCredits());
		lbrCredits.setText("Credits");
		if (u.isMayoffer()) {
			offer.setValue(true);
		} else {
			offer.setValue(false);
		}
		if (u.isMaybid()) {
			bid.setValue(true);
		} else {
			bid.setValue(false);
		}
		//completing panels
		p.add(table);
		
		return p;
	}
}
